//
//  MineViewController.m
//  Peso Guide
//
//  Created by macbook on 2025/6/5.
//

#import "MineViewController.h"
#import "LoginViewController.h"
#import "MineHeaderView.h"
#import "MineViewCell.h"
#import "CustomWebViewController.h"
#import "ProductDetailViewController.h"
#import "MineSettingViewController.h"
#import "MJRefresh/MJRefresh.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) MineHeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if ([AppTools isLogin] == NO) {
        [AppTools toLogin];
        return;
    }
    [self loadMine];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navView.backgroundColor = ColorHex(@"#1A7A35");
    self.backButton.hidden = YES;
    self.titleLabel.text = [Lang set:@"Me"];
    self.backColor = ColorHex(@"#1A7A35");
    
    [self.view addSubview:self.tableView];
    
    WeakSelf;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadMine];
    }];
}

- (void)loadMine {
    [[AWHttpService service] getWithUrl:@"/trains/modified" params:@{} succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            NSDictionary *associating = [NSDictionary safeDictionaryWithDictionary:[responseObject safeObjectForKey:@"associating"]];
            self.dataArray = [NSMutableArray arrayWithArray:[associating safeObjectForKey:@"below"]];
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        
    }];
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *MineViewCellIdentifier = [NSString stringWithFormat:@"MineViewCellIdentifier%ld",(long)indexPath.row];
    MineViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MineViewCellIdentifier];
    if (!cell) {
        cell = [[MineViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MineViewCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.dataArray.count > 0) {
        cell.index = indexPath.row;
        cell.dataDic = [self.dataArray safeObjectAtIndex:indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return (KScreenWidth-40)*0.346+11;
    }
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_headerView == nil) {
        _headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 120+30)];
    }
    [_headerView refreshUserInfo];
    return _headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dataDic = [self.dataArray safeObjectAtIndex:indexPath.row];
    NSString *recent = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"recent"]];

    if ([recent rangeOfString:@"http://"].location != NSNotFound ||
        [recent rangeOfString:@"https://"].location != NSNotFound) {
        //webView
        CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
        webVC.linkUrl = recent;
        [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
        //设置
        MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
        settingVC.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
        return;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
        //首页
        [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
        //登录
        [AppTools toLogin];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
        //订单
        [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
        //产品详情
        NSArray *array = [recent componentsSeparatedByString:@"="];
        NSString *theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
        
        ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
        vc.productId = theId;
        vc.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
        return;
    }
}

#pragma mark - lazy load
- (BaseTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, kStarBarHeight, KScreenWidth, KScreenHeight-kStarBarHeight-kTabbarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = ColorHex(@"#FFFFFF");
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
